﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinHBM.Utils
{
    public class UtilByte
    {

        /// <summary>
        /// 转成Base64字符串进行比较
        /// </summary>
        /// <param name="b1"></param>
        /// <param name="b2"></param>
        /// <returns></returns>
        public static bool BytesCompare_Base64(byte[] b1, byte[] b2)
        {
            if (b1 == null || b2 == null) return false;
            if (b1.Length != b2.Length) return false;
            return string.Compare(Convert.ToBase64String(b1), Convert.ToBase64String(b2), false) == 0 ? true : false;
        }


        /// <summary>
        /// 逐个字节进行比较
        /// </summary>
        /// <param name="b1"></param>
        /// <param name="b2"></param>
        /// <returns></returns>
        public static bool BytesCompare_Step(byte[] b1, byte[] b2)
        {
            if (b1 == null || b2 == null) return false;
            if (b1.Length != b2.Length) return false;
            for (int i = 0; i < b1.Length; ++i)
            {
                if (b1[i] != b2[i]) return false;
            }
            return true;
        }

        public static int ToInt32(byte[] data)
        {
            return (data[0] << 24)+(data[1] << 16) + (data[2] << 8) + data[3];
        }
        public static byte[] GetBytes(int number)
        {
            byte[] abyte0 = new byte[4];   
            abyte0[3] = (byte)(0xff & number); 
            abyte0[2] = (byte)((0xff00 & number) >> 8); 
            abyte0[1] = (byte)((0xff0000 & number) >> 16); 
            abyte0[0] = (byte)((0xff000000 & number) >> 24); 
            return abyte0;
        }
        public static byte[] GetTimeBytes()
        {
            String[] time = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss").Split(new Char[] { '-' });
            String asd = time[0] + "-" + time[1] + "-" + time[2] + " " + time[3] + ":" + time[4] + ":" + time[5];
            byte[] abyte0 = new byte[7];
            abyte0[0] = (byte)((0xff00 & Convert.ToInt32(time[0])) >> 8);
            abyte0[1] = (byte)(0xff & Convert.ToInt32(time[0])); 
            abyte0[2] = (byte)(0xff & Convert.ToInt32(time[1]));
            abyte0[3] = (byte)(0xff & Convert.ToInt32(time[2]));
            abyte0[4] = (byte)(0xff & Convert.ToInt32(time[3]));
            abyte0[5] = (byte)(0xff & Convert.ToInt32(time[4]));
            abyte0[6] = (byte)(0xff & Convert.ToInt32(time[5]));
            Console.WriteLine("时间字符串 ：" + asd);
            Console.WriteLine("时间byte ：" + BitConverter.ToString(abyte0));
            return abyte0;
        }
    }
}
